using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Instedd.Labs.Geo.Library.Rss;
using System.Collections.Generic;

namespace Instedd.Labs.GeoBlogging.WebApplication
{
    public partial class ShowPost : System.Web.UI.Page
    {
        List<Comment> commentsToRender;

        protected override void OnLoadComplete(EventArgs e)
        {
            String postId = Request.QueryString["id"];
            // check for the required parameter being there
            if (String.IsNullOrEmpty(postId))
            {
                return;
            }

            Guid postGuid = Guid.Empty;
            // check for garbage in the Url instead of a valid Guid
            try
            {
                postGuid = new Guid(postId);
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(typeof(ShowPost), "err1", "alert('Invalid Post guid. You will be redirected to the home page now.'); window.location = '/';", true);
                return;
            }

            Item item = DataAccess.GetItem(postGuid);
            commentsToRender = item.Comments;
            // check for non-existent post id
            if (item == null)
            {
                ClientScript.RegisterStartupScript(typeof(ShowPost), "err2", "alert('Post not found. You will be redirected to the home page now.'); window.location = '/';", true);
                return;
            }
            PostTitle.Text = item.Title;
            PostDate.Text = item.PubDate.ToString("r");
            PostDescription.Text = item.Description;
            PostLatLong.Text = String.Format("Decimal {0} || DMS {1}",item.LatLong.ToString("decimal"), item.LatLong.ToString("dms"));
            PostedBy.Text = item.Creator;

            double lat = 0, _long = 0;
            if (item.LatLong != null)
            {
                lat = item.LatLong.Lat;
                _long = item.LatLong.Long;
            }
            Literal1.Text = String.Format("<script language='javascript'>var _lat ={0}; var _long={1};</script>", lat, _long);

            base.OnLoadComplete(e);
        }

        protected override void  OnPreRender(EventArgs e)
        {
            Repeater1.DataSource = commentsToRender;
            Repeater1.DataBind();
            base.OnPreRender(e);
        }

    }
}
